package mysql

import (
	"gorm.io/gorm"
	"manuscript-service/goods/goods"
)

type Goods struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(50);not null"`
	Price   float64 `gorm:"type:decimal(10,2);not null"`
	Adder   string  `gorm:"type:varchar(50)"`
	Stock   int     `gorm:"type:int(11)"`
	ClassId int     `gorm:"type:int(11)"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *Goods) FindGoodsByName(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).First(g).Error
}
func GoodList(db *gorm.DB, data []*goods.GoodsList) []*goods.GoodsList {
	db.Model(Goods{}).
		Select(
			"goods.`name` as good_name",
			"goods.id as good_id",
			"goods.price",
			"goods.adder",
			"goods.stock",
			"classes.`name` as class_name",
			"goods.created_at").
		Joins("JOIN classes ON goods.class_id = classes.id").Find(&data)
	return data
}
